//
//  LSLoginViewController.m
//  Landsea
//
//  Created by leoking870 on 2017/4/20.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "LSLoginViewController.h"
#import "YKUserModel.h"
#import "YKTabBarViewController.h"

@interface LSLoginViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) UITextField *usernameTF;
@property (strong, nonatomic) UITextField *passwordTF;
@property (nonatomic,strong)UIButton* loginBtn;
@end

@implementation LSLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"登录";
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    UIImageView* logoImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_logo"]];
    
    [self.view addSubview:logoImageView];
    
    [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(50);
    }];

    
    UIImageView* icon_user = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ico"]];
    
    self.usernameTF = [[UITextField alloc]init];
    self.usernameTF.tf_id = @"username";
    self.usernameTF.delegate = self;
    self.usernameTF.tf_borderColor = [UIColor lightGrayColor];
    self.usernameTF.tf_borderWidth = 1.0f;
    self.usernameTF.autocorrectionType = UITextAutocorrectionTypeNo;
    self.usernameTF.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.usernameTF.placeholder = @"用户名";
    //    self.usernameTF.keyboardType = UIKeyboardTypePhonePad;
    [self.view addSubview:self.usernameTF];
    [self.usernameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(logoImageView.mas_centerX);
        make.leading.equalTo(self.view.mas_leading).with.offset(20);
        make.trailing.equalTo(self.view.mas_trailing).with.offset(-20);
        make.top.equalTo(logoImageView.mas_bottom).offset(30);
        make.height.mas_equalTo(40);
    }];
    self.usernameTF.returnKeyType = UIReturnKeyNext;
    [icon_user setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [self.usernameTF setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    
    
    
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [paddingView addSubview:icon_user];
    [icon_user mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(paddingView);
    }];
    [self.usernameTF setLeftViewMode:UITextFieldViewModeAlways];
    [self.usernameTF setLeftView:paddingView];
    
    UIImageView* icon_password = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pass"]];
    
    self.passwordTF = [[UITextField alloc]init];
    self.passwordTF.delegate = self;
    [self.view addSubview:self.passwordTF];
    [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.usernameTF.mas_centerX);
        make.leading.equalTo(self.usernameTF.mas_leading);
        make.trailing.equalTo(self.usernameTF.mas_trailing);
        make.top.equalTo(self.usernameTF.mas_bottom).offset(15);
        make.height.mas_equalTo(40);
        
    }];
    self.passwordTF.tf_id = @"password";
    self.passwordTF.returnKeyType = UIReturnKeyDone;
    self.passwordTF.secureTextEntry = YES;
    self.passwordTF.tf_borderColor = [UIColor lightGrayColor];
    self.passwordTF.tf_borderWidth = 1.0f;
    self.passwordTF.placeholder = @"请输入密码";
    [icon_password setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [self.passwordTF setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    
    
    paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [paddingView addSubview:icon_password];
    [icon_password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(paddingView);
    }];
    [self.passwordTF setLeftViewMode:UITextFieldViewModeAlways];
    [self.passwordTF setLeftView:paddingView];
    
    self.loginBtn = [[UIButton alloc]init];
    [self.loginBtn setTitle:@"立即登录" forState:UIControlStateNormal];
    [self.loginBtn setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [self.loginBtn setBackgroundImage:TFSTYLE(navigationBarBackgroudImage) forState:UIControlStateNormal];
    [self.loginBtn setBackgroundImage:TFSTYLE(buttonDisableBackgroundImage) forState:UIControlStateDisabled];
    self.loginBtn.contentEdgeInsets = UIEdgeInsetsMake(10, 0, 10, 0);
    [self.view addSubview:self.loginBtn];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordTF.mas_bottom).with.offset(60);
        make.leading.equalTo(self.passwordTF.mas_leading);
        make.trailing.equalTo(self.passwordTF.mas_trailing);
        make.height.equalTo(self.passwordTF.mas_height);
    }];
    self.loginBtn.titleLabel.font = [UIFont boldSystemFontOfSize:22];
    
    //    [self.loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    self.loginBtn.tf_cornerRadius = 5.0f;
    self.usernameTF.text = [[NSUserDefaults standardUserDefaults]stringForKey:@"username"];
    self.passwordTF.text = [[NSUserDefaults standardUserDefaults]stringForKey:@"password"];
//    self.usernameTF.text = @"admin";
//    self.passwordTF.text = @"123456";
    RACSignal* signal = [RACSignal combineLatest:@[self.usernameTF.rac_textSignal, self.passwordTF.rac_textSignal] reduce:^id(NSString* username, NSString* password){
        return @(username.length && password.length);
    }];
    
    self.loginBtn.rac_command = [[RACCommand alloc]initWithEnabled:signal signalBlock:^RACSignal *(id input) {
        [self login:input];
        return [RACSignal empty];
    }];
    
    
    UILabel* versionLabel = [[UILabel alloc]init];
    versionLabel.text = [NSString stringWithFormat:@"版本:%@",[[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];
    versionLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:versionLabel];
    [versionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loginBtn.mas_bottom).offset(15);
        make.centerX.equalTo(self.loginBtn.mas_centerX);
    }];
    
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    UIView* username = [self.view tf_findViewById:@"username"];
    username.tf_cornerRadius = username.tf_height / 2.0f;
    
    UIView* password = [self.view tf_findViewById:@"password"];
    password.tf_cornerRadius = password.tf_height / 2.0f;
    
    self.loginBtn.tf_cornerRadius = self.loginBtn.tf_height / 2.0f;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (self.usernameTF == textField) {
        [self.passwordTF becomeFirstResponder];
    }
    else{
        [self.passwordTF resignFirstResponder];
        [self login:self.loginBtn];
    }
    return YES;
}

- (void)login:(id)sender {
    if (self.usernameTF.text.length == 0) {
        [self showHint:@"请填入用户名"];
    }
    if (self.usernameTF.text.length == 0) {
        [self showHint:@"请填入密码"];
    }
    [self.view endEditing:YES];
    {
        [[NSUserDefaults standardUserDefaults]setValue:self.usernameTF.text forKey:@"username"];
        [[NSUserDefaults standardUserDefaults]setValue:self.passwordTF.text forKey:@"password"];
        
        [TFHTTPMANAER POST:@"login/userLogin.htm"
                parameters:@{@"userId":self.usernameTF.text,
                             @"password": self.passwordTF.text,
                             @"register_id": [[YKTabBarViewController sharedInstance] register_id] ?:@""}
                       HUD:@"正在登录..."
                   success:^(id responseObject) {
                       YKUserModel* user = [YKUserModel yy_modelWithJSON:responseObject[@"userMap"]];
                       if (self.delegate && [self.delegate respondsToSelector:@selector(loginViewDidLoginSuccessWithUser:)]) {
                           [self.delegate loginViewDidLoginSuccessWithUser:user];
                       }
                   }
                   failure:^(id responseObject, NSError *error) {
                       if (error.code == TFHttpDataValidatorFailedErrorCode) {
                           [self showHint:error.userInfo[NSLocalizedFailureReasonErrorKey]];
                       }
                       else {
                           [self showHint:error.localizedDescription];
                       }
                   }];
    }
}
-(void)onLoginCancled:(id)psender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(loginCancled)]) {
        [self.delegate loginCancled];
    }
    
}
@end
